﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace EmailJabber.EmailJabber.Core.FileManagemant
{
    public class FileSystemBlo
    {
        static private FileSystemBlo MySingleObj = null;

        protected FileSystemBlo()
        {
            CurrentDir = ".";
            CurrentEncoding = Encoding.GetEncoding("windows-1251");
        }

        public static FileSystemBlo Instance
        {
            get
            {
                if (MySingleObj == null)
                    MySingleObj = new FileSystemBlo();

                return MySingleObj;
            }
        }

        public string CurrentDir { get; set; }

        public Encoding CurrentEncoding { get; set; }

        public string ChangeDir(string newDir)
        {
            if (newDir.Contains(":\\"))
            {
                if (Directory.Exists(newDir))
                {
                    CurrentDir = newDir;
                }
            }
            else if (newDir.Trim().Equals(".."))
            {
                if (Directory.GetParent(CurrentDir).Exists)
                    CurrentDir = Directory.GetParent(CurrentDir).FullName;
            }
            else if (Directory.Exists(string.Format("{0}\\{1}", CurrentDir, newDir)))
            {
                CurrentDir = string.Format("{0}\\{1}", CurrentDir, newDir);
            }
            return CurrentDir;
        }
    }
}
